﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFP;
using SFP.Animation2D;
using SFP.CollisionDetection;

namespace TestGame
{
    public class Obstacle : GlobalReaction
    {
        public Player player;
        public LayerManager layerManager;

        public Obstacle(Player player, LayerManager layerManager)
        {
            this.player = player;
            this.layerManager = layerManager;
        }

        private CollisionInfo collisionMaxOverlap(HashSet<CollisionInfo> collisionInfos)
        {
            CollisionInfo collisionMaxOvelap = null;

            foreach (CollisionInfo collisionInfo in collisionInfos)
            {
                if (collisionMaxOvelap == null)
                {
                    collisionMaxOvelap = collisionInfo;
                }
                else
                {
                    if (collisionInfo.Overlap > collisionMaxOvelap.Overlap)
                    {
                        collisionMaxOvelap = collisionInfo;
                    }
                }
            }

            return collisionMaxOvelap;
        }

        protected override void fromBeneath(HashSet<CollisionInfo> collisionInfos)
        {
            CollisionInfo collisionInfo = collisionMaxOverlap(collisionInfos);

            player.VelocityY = 0;
            player.Jump = false;
            
            if (layerManager.isYAtBeginn() || layerManager.isYAtEnd())
                player.Y += collisionInfo.Overlap;
            else
                layerManager.scrollY(-collisionInfo.Overlap);
        }

        protected override void fromAbove(HashSet<CollisionInfo> collisionInfos)
        {
            CollisionInfo collisionInfo = collisionMaxOverlap(collisionInfos);

            player.GroundCollisionFound = true;
            player.OnGround = true;
            player.Jump = false;

            if (layerManager.isYAtBeginn() || layerManager.isYAtEnd())
                player.Y -= collisionInfo.Overlap;
            else
                layerManager.scrollY(collisionInfo.Overlap);
        }

        protected override void fromLeft(HashSet<CollisionInfo> collisionInfos)
        {
            CollisionInfo collisionInfo = collisionMaxOverlap(collisionInfos);

            player.VelocityX = 0;
            if (layerManager.isXAtBeginn() || layerManager.isXAtEnd())
                player.X -= collisionInfo.Overlap;
            else
                layerManager.scrollX(-collisionInfo.Overlap);
        }

        protected override void fromRight(HashSet<CollisionInfo> collisionInfos)
        {
            CollisionInfo collisionInfo = collisionMaxOverlap(collisionInfos);

            player.VelocityX = 0;
            if (layerManager.isXAtBeginn() || layerManager.isXAtEnd())
                player.X += collisionInfo.Overlap;
            else
                layerManager.scrollX(collisionInfo.Overlap);
        }

        protected override void touchesTop(HashSet<CollisionInfo> collisionInfos)
        {
            player.GroundCollisionFound = true;
            player.OnGround = true;
            player.Jump = false;
        }

        protected override void touchesBottom(HashSet<CollisionInfo> collisionInfos)
        {
        }

        protected override void touchesLeft(HashSet<CollisionInfo> collisionInfos)
        {
        }

        protected override void touchesRight(HashSet<CollisionInfo> collisionInfos)
        {
        }
    }
}
